/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.io;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletOutputStream;

/**
 * This class wrapps a <code>OutputStream</code> into a
 * <code>DumbServletOutputStream</code>. It delegates all its operation to
 * the underlying <code>OutputStream</code>.
 */
class DumbServletOutputStream
    extends ServletOutputStream
{
  /**
   * the <code>OutputStream</code> to wrapp.
   */
  private transient final OutputStream out;



  /**
   * Constructor.
   * 
   * @param out
   *          the <code>OutputStream</code> to wrapp
   */
  public DumbServletOutputStream(final OutputStream out)
  {
    this.out = out;
  }



  /**
   * Closes this output stream and releases any system resources associated with
   * this stream. The general contract of <code>close</code> is that it closes
   * the output stream. A closed stream cannot perform output operations and
   * cannot be reopened.
   * 
   * @throws IOException
   *           if an I/O error occurs.
   */
  @Override
  public void close()
      throws IOException
  {
    out.close();
  }



  /**
   * Flushes this output stream and forces any buffered output bytes to be
   * written out. The general contract of <code>flush</code> is that calling
   * it is an indication that, if any bytes previously written have been
   * buffered by the implementation of the output stream, such bytes should
   * immediately be written to their intended destination.
   * 
   * @throws IOException
   *           if an I/O error occurs.
   */
  @Override
  public void flush()
      throws IOException
  {
    out.flush();
  }



  /**
   * Returns a string representation of the object. In general, the
   * <code>toString</code> method returns a string that "textually represents"
   * this object. The result should be a concise but informative representation
   * that is easy for a person to read.
   * 
   * @return a string representation of the object.
   */
  @Override
  public String toString()
  {
    return out.toString();
  }



  /**
   * Writes <code>len</code> bytes from the specified byte array starting at
   * offset <code>off</code> to this output stream. The general contract for
   * <code>write(b, off, len)</code> is that some of the bytes in the array
   * <code>b</code> are written to the output stream in order; element
   * <code>b[off]</code> is the first byte written and
   * <code>b[off+len-1]</code> is the last byte written by this operation.
   * 
   * @param buffer
   *          the data.
   * @param off
   *          the start offset in the data.
   * @param len
   *          the number of bytes to write.
   * @throws IOException
   *           if an I/O error occurs. In particular, an
   *           <code>IOException</code> is thrown if the output stream is
   *           closed.
   */
  @Override
  public void write(final byte[] buffer, final int off, final int len)
      throws IOException
  {
    out.write(buffer, off, len);
  }



  /**
   * Writes <code>b.length</code> bytes from the specified byte array to this
   * output stream. The general contract for <code>write(b)</code> is that it
   * should have exactly the same effect as the call
   * <code>write(b, 0, b.length)</code>.
   * 
   * @param buffer
   *          the data.
   * @throws IOException
   *           if an I/O error occurs.
   */

  @Override
  public void write(final byte[] buffer)
      throws IOException
  {
    out.write(buffer);
  }



  /**
   * Writes the specified byte to this output stream. The general contract for
   * <code>write</code> is that one byte is written to the output stream. The
   * byte to be written is the eight low-order bits of the argument
   * <code>b</code>. The 24 high-order bits of <code>b</code> are ignored.
   * 
   * @param simpleByte
   *          the <code>byte</code>.
   * @throws IOException
   *           if an I/O error occurs. In particular, an
   *           <code>IOException</code> may be thrown if the output stream has
   *           been closed.
   */
  @Override
  public void write(final int simpleByte)
      throws IOException
  {
    out.write(simpleByte);
  }
}
